package com.avantis.admin.service.impl;

import com.avantis.admin.service.SupplierService;
import com.avantis.common.base.BaseServiceImpl;
import com.avantis.common.entity.Supplier;
import com.avantis.common.entity.query.SupplierQuery;
import com.avantis.common.exception.ServiceProcessException;
import com.avantis.common.msg.YResult;
import com.avantis.common.repository.SupplierRepository;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* @Description:    java类作用描述
* @Author:         kirago
* @CreateDate:     2019/2/13 10:42 AM
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@Service
@Transactional
public class SupplierServiceImpl extends BaseServiceImpl<SupplierRepository, Supplier> implements SupplierService {

    @Autowired
    private SupplierRepository supplierRepository;

    @Autowired
    private SupplierService supplierService;

    @Override
    public void saveSupplier(Supplier supplier) {
        try {
            save(supplier);
        } catch (ServiceProcessException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceProcessException("新增供应商失败！");
        }
    }

    @Override
    public void modifySupplier(Supplier supplier) {
        supplierRepository.saveAndFlush(supplier);
    }

    @Override
    public void deleteSupplier(String supplierId) {
        supplierRepository.deleteByUuid(supplierId);
    }

    @Override
    public Page<Supplier> findSupplierCriteria(SupplierQuery supplierQuery) {
        Pageable pageable = new PageRequest(supplierQuery.getPageNumber() - 1, supplierQuery.getPageSize());
        Page<Supplier> supplierPage = supplierRepository.findAll(new Specification<Supplier>() {

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotEmpty(supplierQuery.getSupplierName())) {
                    predicates.add(criteriaBuilder.like(root.get("supplierName").as(String.class), "%" + supplierQuery.getSupplierName() + "%"));
                }
                if (StringUtils.isNotEmpty(supplierQuery.getSupplierCode())) {
                    predicates.add(criteriaBuilder.like(root.get("supplierCode").as(String.class), "%" + supplierQuery.getSupplierCode() + "%"));
                }
                if (StringUtils.isNotEmpty(supplierQuery.getSupplierPhone())) {
                    predicates.add(criteriaBuilder.like(root.get("supplierPhone").as(String.class), "%" + supplierQuery.getSupplierPhone() + "%"));
                }
                // 如果 supplierlevel 为 0 的话就是查找全部的 level
                if (supplierQuery.getSupplierLevel() > 0 ) {
                    predicates.add(criteriaBuilder.equal(root.get("supplierLevel").as(String.class), supplierQuery.getSupplierLevel()));
                }
                criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
                return criteriaQuery.getRestriction();
            }
        }, pageable);

        return supplierPage;
    }

    @Override
    public Supplier findOne(String supplierId) {
        Supplier supplier = supplierRepository.findByUuid(supplierId);
        return supplier;
    }
}
